package kr.hyunist.androidcomplete01saeildamil.ch19;

import kr.hyunist.androidcomplete01saeildamil.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class ThreadHadlerEx03 extends Activity {
	int mMainValue = 0;
	int mBackValue = 0;
	TextView mMainText;
	TextView mBackText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.threadtest01);

		mMainText = (TextView) findViewById(R.id.threadtesttv1);
		mBackText = (TextView) findViewById(R.id.threadtesttv2);

		BackRunnable r = new BackRunnable();
		Thread t = new Thread(r);
		t.setDaemon(true);
		t.start();
	}

	public void mOnClick(View v) {
		mMainValue++;
		mMainText.setText("MainValue : " + mMainValue);
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				mBackText.setText("BackValue : " + mBackValue);
			}
		}

	};

	class BackRunnable implements Runnable {

		@Override
		public void run() {
			while (true) {
				mBackValue++;
				mHandler.sendEmptyMessage(0);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {

				}
			}
		}

	}
}
